/**
 * 用于获取后端传回来用户的具体信息
 * 和token
 */
import { userinfo } from '@/stores/counter';
import type { BaseInfo } from '@/stores/counter';
import subscribeCate from '@/api/subscribeCate';
export default function getUserInfo() {
  const user = userinfo();
  function getInfo(userinfo: BaseInfo) {
    user.baseInfo.id = userinfo.id;
    user.baseInfo.username = userinfo.username;
    user.baseInfo.telephone = userinfo.telephone;
    user.baseInfo.token = userinfo.token;
    user.baseInfo.avator = userinfo.avator;
    user.baseInfo.sex = userinfo.sex;
    user.baseInfo.mail = userinfo.mail;
    //记得加评论
  }

  async function setInfo() {
    user.login = true;
    localStorage.setItem('id', user.baseInfo.id);
    localStorage.setItem('username', user.baseInfo.username);
    localStorage.setItem('telephone', user.baseInfo.telephone);
    localStorage.setItem('sex', user.baseInfo.sex);
    localStorage.setItem('token', user.baseInfo.token);
    localStorage.setItem('avator', user.baseInfo.avator);
    localStorage.setItem('mail', user.baseInfo.mail);
  }

  async function loadInfo() {
    const { getSubscribeList } = subscribeCate();
    user.baseInfo.id = localStorage.getItem('id');
    user.baseInfo.username = localStorage.getItem('username');
    user.baseInfo.telephone = localStorage.getItem('telephone');
    user.baseInfo.sex = localStorage.getItem('sex');
    user.baseInfo.token = localStorage.getItem('token');
    user.baseInfo.avator = localStorage.getItem('avator');
    user.baseInfo.mail = localStorage.getItem('mail');
    user.login = true;
    getSubscribeList();
  }

  return {
    getInfo,
    setInfo,
    loadInfo,
  };
}
